package com.lp.chapter001.handler.easypoi;

import cn.afterturn.easypoi.handler.inter.IExcelExportServer;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.lp.chapter001.entity.Student;
import com.lp.chapter001.excel.easypoi.EasypoiExcel;
import com.lp.chapter001.mapper.StudentMapper;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * easypoi 学生信息导出处理器
 * @author LP to 2024/03/06
 */
@Service
public class EasypoiStudentExportHandler implements IExcelExportServer {
    /**
     * 每次分页查询条数
     */
    private final static Integer PAGE_SIZE = 100000;
    @Resource
    private StudentMapper studentMapper;

    /**
     * 查询数据接口
     * @param queryParams 查询条件
     * @param page        当前页数从1开始
     */
    @Override
    public List<Object> selectListForExcelExport(Object queryParams, int page) {
        Page<Student> pageStu = new Page<>(page, PAGE_SIZE);
        QueryWrapper<Student> qw = new QueryWrapper<>();
        qw.select("name", "student_num", "age", "sex", "birthday");
        qw.orderByAsc("student_num");
        Page<Student> studentPage = studentMapper.selectPage(pageStu, qw);
        List<Object> resultObjLs = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(studentPage.getRecords())) {
            resultObjLs.addAll(
                    studentPage.getRecords().stream().map(o -> {
                        EasypoiExcel excel = new EasypoiExcel();
                        excel.setName(o.getName());
                        excel.setStudentNum(o.getStudentNum());
                        excel.setAge(o.getAge());
                        excel.setSex(o.getSex());
                        excel.setBirthday(o.getBirthday());
                        return excel;
                    }).toList()
            );
        }
        return resultObjLs;
    }
}
